<?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Product\Form\CatalogDetailForm;
use Product\Model\ProductTemplate;
use Product\Form\CatalogForm;
use Product\Form\ProductTemplateForm;

// use Product\Form\ProductCatalogForm;
class CatalogController extends AbstractActionController {
	protected $catalogTable;
	public function indexAction() {
		return new ViewModel ( array (
				'catalogs' => $this->getCatalogTable ()->fetchAll () 
		) );
	}
	public function addAction() {
		$form = new ProductTemplateForm ();
		$form->get ( 'submit' )->setValue ( 'Add' );
		
		$request = $this->getRequest();
		if ($request->isPost()) {
			$product = new ProductTemplate();
			$form->setInputFilter($product->getInputFilter());
			$form->setData ( $request->getPost() );
			
			if ($form->isValid()) {
				echo 'valid';
				$product->exchangeArray ( $form->getData() );
				$this->getProductTemplateTable()->saveProduct($product );
				
				// Redirect to list of albums
				return $this->redirect ()->toRoute ( 'productcatalog' );
			}
		}
		return array (
				'form' => $form 
		);
	}
	public function editAction()
     {
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('productcatalog', array(
                 'action' => 'add'
             ));
         }

         // Get the Album with the specified id.  An exception is thrown
         // if it cannot be found, in which case go to the index page.
         try {
             $product = $this->getProductTemplateTable()->getProduct($id);
         }
         catch (\Exception $ex) {
             return $this->redirect()->toRoute('productcatalog', array(
                 'action' => 'index'
             ));
         }

         $form  = new ProductTemplateForm();
         $form->bind($product);
         $form->get('submit')->setAttribute('value', 'Edit');
		echo 'valid';
         $request = $this->getRequest();
         if ($request->isPost()) {
			
             $form->setInputFilter($product->getInputFilter());
             $form->setData($request->getPost());

             if ($form->isValid()) {
			 
                 $this->getCategoryTable()->saveProductTemplate($product);

                 // Redirect to list of albums
                 return $this->redirect()->toRoute('productcatalog');
             }
         }

         return array(
             'id' => $id,
             'form' => $form,
         );
     }
	public function detailAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'productcatalog', array (
					'action' => 'index' 
			) );
		}
		
		// Get the Album with the specified id. An exception is thrown
		// if it cannot be found, in which case go to the index page.
		try {
			$product = $this->getDetailCatalogTable ()->getCatalog ( $id );
		} catch ( \Exception $ex ) {
			return $this->redirect ()->toRoute ( 'productcatalog', array (
					'action' => 'index' 
			) );
		}
		
		$form = new CatalogDetailForm ();
		$form->bind ( $product );
		
		return array (
				'id' => $id,
				'form' => $form 
		);
	}
	public function getCatalogTable() {
		if (! $this->catalogTable) {
			$sm = $this->getServiceLocator ();
			$this->catalogTable = $sm->get ( 'Product\Model\CatalogTable' );
		}
		return $this->catalogTable;
	}
	public function getProductTemplateTable() {
		if (! $this->catalogTable) {
			$sm = $this->getServiceLocator ();
			$this->catalogTable = $sm->get ( 'Product\Model\ProductTemplateTable' );
		}
		return $this->catalogTable;
	}
	
	public function getDetailCatalogTable() {
		if (! $this->catalogTable) {
			$sm = $this->getServiceLocator ();
			$this->catalogTable = $sm->get ( 'Product\Model\DetailCatalogTable' );
		}
		return $this->catalogTable;
	}
	public function deleteAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'productcatalog' );
		}
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$del = $request->getPost ( 'del', 'No' );
			
			if ($del == 'Yes') {
				$id = ( int ) $request->getPost ( 'id' );
				$this->getProductCatalogTable ()->deleteProductCatalog ( $id );
			}
			
			return $this->redirect ()->toRoute ( 'productcatalog' );
		}
		
		return array (
				'id' => $id,
				'catalog' => $this->getProductCatalogTable ()->getProductCatalog ( $id ) 
		);
	}
	public function getProductCatalogTable() {
		if (! $this->catalogTable) {
			$sm = $this->getServiceLocator ();
			$this->catalogTable = $sm->get ( 'Product\Model\ProductCatalogTable' );
		}
		return $this->catalogTable;
	}
}